package com.anyou.mall.supply.controller;

import java.time.LocalDate;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollUtil;
import com.anyou.mall.goo.goods.domain.GooGoods;
import com.anyou.mall.goo.goods.domain.GoodsVo2;
import com.anyou.mall.goo.goods.domain.LiGoodsSku;
import com.anyou.mall.order.service.IOrdOrderService;
import com.anyou.mall.platform.domain.OrdDelivery;
import com.anyou.mall.platform.service.IOrdDeliveryService;
import com.anyou.mall.supply.domain.SupplyTime;
import com.anyou.mall.supply.service.ISupplyTimeService;
import com.anyou.mall.supply.vo.GoodsVo;
import com.anyou.mall.supply.vo.SupplyVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.anyou.common.annotation.Log;
import com.anyou.common.core.controller.BaseController;
import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.anyou.mall.supply.domain.Supply;
import com.anyou.mall.supply.service.ISupplyService;
import com.anyou.common.utils.poi.ExcelUtil;
import com.anyou.common.core.page.TableDataInfo;

/**
 * 补给单Controller
 *
 * @author ruoyi
 * @date 2023-10-13
 */
@Api(tags = "补给订单")
@RestController
@RequestMapping("/supply/supply")
public class SupplyController extends BaseController
{
    @Autowired
    private ISupplyService supplyService;
    @Autowired
    private ISupplyTimeService supplyTimeService;
    @Autowired
    private IOrdOrderService ordOrderService;
    @Autowired
    private IOrdDeliveryService ordDeliveryService;


    /**
     * 查询补给单列表
     */
    @ApiOperation("查询补给单列表")
    @PreAuthorize("@ss.hasPermi('supply:supply:list')")
    @GetMapping("/list")
    public TableDataInfo<List<Supply>> list(Supply supply)
    {
        startPage();
        List<Supply> list = supplyService.selectSupplyList(supply);

        return getDataTable(list);
    }



    /**
     * 补给订单添加页面
     * 规格列表
     */
    @ApiOperation("订单添加-规格列表")
    @PreAuthorize("@ss.hasPermi('order:ord:specList')")
    @GetMapping(value = "/specList")
    public ApiResult<List<LiGoodsSku>> specList(@RequestParam("goodsId") Long goodsId)
    {
        List<LiGoodsSku> list = ordOrderService.specsListByGoodsId(goodsId);
        return ApiResult.success(list);
    }
    /**
     * 补给订单添加页面
     * 商品列表
     */
    @ApiOperation("订单添加-商品列表")
    @PreAuthorize("@ss.hasPermi('order:ord:goodsList')")
    @GetMapping(value = "/goodsList")
    public ApiResult<List<GoodsVo2>> goodsList(@RequestParam(value = "goodsName",required = false) String goodsName)
    {
        List<GoodsVo2> list = ordOrderService.goodsListByGoodsName(goodsName);
        return ApiResult.success(list);
    }

    /**
     * 导出补给单列表
     */
    @ApiOperation("导出补给单列表")
    @PreAuthorize("@ss.hasPermi('supply:supply:export')")
    @Log(title = "补给单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Supply supply)
    {
        List<Supply> list = supplyService.selectSupplyList(supply);
        ExcelUtil<Supply> util = new ExcelUtil<Supply>(Supply.class);
        util.exportExcel(response, list, "补给单数据");
    }

    /**
     * 获取补给单详细信息
     */
    @ApiOperation("获取补给单详细信息")
    @PreAuthorize("@ss.hasPermi('supply:supply:query')")
    @GetMapping(value = "/{supplyId}")
    public ApiResult<SupplyVo> getInfo(@PathVariable("supplyId") Long supplyId)
    {
        return ApiResult.success(supplyService.selectSupplyBySupplyId(supplyId));
    }
    /**
     * 开始配送
     */
    @ApiOperation("开始配送")
    @PreAuthorize("@ss.hasPermi('supply:supply:startPs')")
    @GetMapping(value = "/startPs")
    public ApiResult startPs(@RequestParam("supplyId") Long supplyId)
    {
        supplyService.startPs(supplyId);
        return ApiResult.success();
    }

    /**
     * 新增补给单
     */
    @ApiOperation("新增补给单")
    @PreAuthorize("@ss.hasPermi('supply:supply:add')")
    @Log(title = "补给单", businessType = BusinessType.INSERT)
    @PostMapping
    public ApiResult add(@RequestBody Supply supply)
    {
        return ApiResult.success(supplyService.insertSupply(supply));
    }
    /**
     * 查询桶押金
     */
    @ApiOperation("查询桶押金")
    @PreAuthorize("@ss.hasPermi('supply:supply:bucketmoney')")
    @Log(title = "补给单", businessType = BusinessType.INSERT)
    @PostMapping("/getBucketMoney")
    public ApiResult bucketmoney()
    {
        List<OrdDelivery> list = ordDeliveryService.list();
        if(CollUtil.isNotEmpty(list)){
            return ApiResult.success(list.get(0).getBucketDetainPrice());
        }
        return ApiResult.success(0);
    }
    /**
     * 修改下次配送时间
     */
    @ApiOperation("修改下次配送时间")
    @PreAuthorize("@ss.hasPermi('supply:supply:editTime')")
    @Log(title = "修改下次配送时间", businessType = BusinessType.INSERT)
    @PostMapping("/editTime")
    public ApiResult editTime(@RequestParam("date")LocalDate date)
    {
        supplyService.editTime(date);
        return ApiResult.success();
    }

    /**
     * 修改补给单
     */
    @ApiOperation("修改补给单")
    @PreAuthorize("@ss.hasPermi('supply:supply:edit')")
    @Log(title = "补给单", businessType = BusinessType.UPDATE)
    @PutMapping
    public ApiResult edit(@RequestBody Supply supply)
    {
        return ApiResult.success(supplyService.updateSupply(supply));
    }

    /**
     * 删除补给单
     */
    @ApiOperation("删除补给单")
    @PreAuthorize("@ss.hasPermi('supply:supply:remove')")
    @Log(title = "补给单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{supplyIds}")
    public ApiResult remove(@PathVariable Long[] supplyIds)
    {
        return ApiResult.success(supplyService.deleteSupplyBySupplyIds(supplyIds));
    }
}
